/*
@title: music-be-like
@description: Very Good Music™. Nothing strange here.
@author: trueGrand
@tags: ['puzzle']
@addedOn: 2022-12-04

Instructions:

Welcome to Sprig!!!

----------
Totally not a Rick Roll
----------


*/


const player = "p";
const box = "b";
const goal = "g";
const wall = "w";
const background = "v"
const rickroll1 = "r"
const rickroll2 = "s"
const rickroll3 = "w"

setLegend(
  [ player, bitmap`
................
................
................
................
.....2222.......
.....2022.......
.....2222.......
.....2202.......
.....2222.......
...222222222....
...222222222....
...222222222....
...222222222....
...222222222....
...222222222....
...222222222....`],
  [ box, bitmap`
................
................
................
...CCCCCCCCCCC..
...C....C....C..
...C....C....C..
...C....C....C..
...C....C....C..
...CCCCCCCCCCC..
...C....C....C..
...C....C....C..
...C....C....C..
...C....C....C..
...CCCCCCCCCCC..
................
................`],
  [ goal, bitmap`
................
................
................
.......6........
.......6........
.......66.......
...6666666666...
...6..6.6..6....
....6.6..66.....
.....66..6......
......6666......
.....6.666......
.....66..66.....
.....6....6.....
................
................`],
  [ wall, bitmap`
2222222222222222
2000000000000002
2000000000000002
2000000000000002
2000000000000002
2000000000000002
2000003333000002
2000003333000002
2000003333000002
2000003333000002
2000000000000002
2000000000000002
2000000000000002
2000000000000002
2000000000000002
2222222222222222`],
  [ background, bitmap`
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888`],
  [ rickroll1, bitmap`
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888`],
  [ rickroll2, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
  [ rickroll3, bitmap`
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999`]
);

setBackground(background)

let level = 0;
const levels = [
  map`
rrrrrrrr
rrrwwrrr
rrrwwrrr
rrssssrr
rrsssssr
rrsssssr
rsssssrr
psssssrr`,
];

const currentLevel = levels[level];
setMap(currentLevel);

setSolids([ player, box, wall ]);

setPushables({
  [player]: [box],
  [box]: [box]
});

// START - PLAYER MOVEMENT CONTROLS

onInput("s", () => {
  getFirst(player).y += 1;
});

onInput("d", () => {
  getFirst(player).x += 1;
});

onInput("w", () => {
  getFirst(player).y -= 1;
});

onInput("a", () => {
  getFirst(player).x -= 1;
});

// END - PLAYER MOVEMENT CONTROLS

onInput("j", () => {
  const currentLevel = levels[level];
  if (currentLevel !== undefined) {
    clearText("");
    setMap(currentLevel);
  }
});

afterInput(() => {
  // count the number of tiles with goals
  const targetNumber = tilesWith(goal).length;
  
  // count the number of tiles with goals and boxes
  const numberCovered = tilesWith(goal, box).length;

  if (numberCovered === targetNumber) {
    // increase the current level number
    level = level + 1;

    const currentLevel = levels[level];

    // make sure the level exists and if so set the map
    if (currentLevel !== undefined) {
      setMap(currentLevel);
    } else {
      addText("Get Rick Rolled", { y: 4, color: color`2` });
    }
  }
});

//tune
const melody = tune`
428.57142857142856,
214.28571428571428: g4~214.28571428571428 + c5~214.28571428571428,
214.28571428571428,
214.28571428571428: g4/214.28571428571428 + c5/214.28571428571428,
214.28571428571428,
214.28571428571428: g4~214.28571428571428 + c5~214.28571428571428,
214.28571428571428,
214.28571428571428: g4/214.28571428571428 + c5/214.28571428571428,
214.28571428571428,
214.28571428571428: a4~214.28571428571428 + d5~214.28571428571428,
214.28571428571428,
214.28571428571428: a4/214.28571428571428 + d5/214.28571428571428,
214.28571428571428,
214.28571428571428: a4~214.28571428571428 + d5~214.28571428571428,
214.28571428571428,
214.28571428571428: a4/214.28571428571428 + d5/214.28571428571428,
214.28571428571428,
214.28571428571428: b4~214.28571428571428 + e5~214.28571428571428,
214.28571428571428,
214.28571428571428: b4/214.28571428571428 + e5/214.28571428571428,
214.28571428571428,
214.28571428571428: b4~214.28571428571428 + e5~214.28571428571428,
214.28571428571428,
214.28571428571428: b4/214.28571428571428 + e5/214.28571428571428,
214.28571428571428,
214.28571428571428: c5-214.28571428571428 + e5~214.28571428571428,
214.28571428571428: c5-214.28571428571428 + e5~214.28571428571428,
214.28571428571428: c5-214.28571428571428 + e5~214.28571428571428,
214.28571428571428: c5-214.28571428571428 + e5~214.28571428571428,
428.57142857142856`
const rickroll = tune`
263.1578947368421,
263.1578947368421: c4-263.1578947368421,
263.1578947368421: d4-263.1578947368421,
263.1578947368421: f4-263.1578947368421,
263.1578947368421: d4-263.1578947368421,
263.1578947368421: a4-263.1578947368421,
263.1578947368421: a4-263.1578947368421,
263.1578947368421: g4-263.1578947368421,
263.1578947368421: c4-263.1578947368421,
263.1578947368421: d4-263.1578947368421,
263.1578947368421: f4-263.1578947368421,
263.1578947368421: d4-263.1578947368421,
263.1578947368421: g4-263.1578947368421,
263.1578947368421: g4-263.1578947368421,
263.1578947368421: f4-263.1578947368421,
263.1578947368421: c4-263.1578947368421,
263.1578947368421: d4-263.1578947368421,
263.1578947368421: f4-263.1578947368421,
263.1578947368421: d4-263.1578947368421,
263.1578947368421: f4-263.1578947368421,
263.1578947368421: g4-263.1578947368421,
263.1578947368421: e4-263.1578947368421,
263.1578947368421: d4-263.1578947368421,
263.1578947368421: c4-263.1578947368421,
263.1578947368421: g4-263.1578947368421,
263.1578947368421: f4-263.1578947368421,
1578.9473684210525`
playTune(rickroll, Infinity)
